package com.fishery.processing.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.processing.entity.Input;
import com.fishery.processing.mapper.InputMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author run
 * @since 2021/1/14 16:42
 */
@Component
public class InputDao {

    @Autowired
    private InputMapper inputMapper;

    public int addInput(Input input){
        return inputMapper.insert(input);
    }

    public int remove(String inputId){
        return inputMapper.deleteById(inputId);
    }

    public int update(Input input){
        return inputMapper.updateById(input);
    }

    public Input findInputByCraftAndInput(String craftId,String inputId){
        return inputMapper.findInputByCraftAndInput(craftId,inputId);
    }

    public Input findInputById(String inputId){
        return inputMapper.selectById(inputId);
    }

    public List<Input> findInputByFactoryAndName(Input input){
        Map<String,Object> map = new HashMap<>(2,1);
        map.put("input_name",input.getInputName());
        map.put("processing_factory_id",input.getProcessingFactoryId());
        return inputMapper.selectByMap(map);
    }

    //分页查找加工厂的投入品信息
    public IPage<Input> findInputsOfFactory(String factoryId, Integer page, Integer size){
        QueryWrapper<Input> wrapper = new QueryWrapper<>();
        wrapper.eq("processing_factory_id",factoryId);
        Page<Input> inputPage = new Page<>(page,size);
        return inputMapper.selectPage(inputPage, wrapper);
    }

    //(全部)查找加工厂的投入品信息
    public List<Input> findInputsOfFactory(String factoryId){
        Map<String,Object> map = new HashMap<>(1,1);
        map.put("processing_factory_id",factoryId);
        return inputMapper.selectByMap(map);
    }

    //分页查找工艺的投入品信息
    public IPage<Input> findInputsOfCraft(String craftId,Integer page,Integer size){
        Page<Input> inputIPage = new Page<>(page,size);
        long total = inputMapper.findInputCountOfCraft(craftId);
        return inputIPage.setRecords(inputMapper.findInputsByCraft(craftId, Math.max((page - 1) * size,0), size))
                            .setTotal(total)
                            .setPages((int)Math.ceil(total / (size * 1.0)));
    }

    //(全部)查找工艺的投入品信息
    public List<Input> findInputsOfCraft(String craftId){
        return inputMapper.findAllInputsByCraft(craftId);
    }

    //删除加工厂的所有投入品信息
    public int removeInputOfFactory(String factoryId){
        //删除加工厂所有投入品的连接表信息
        inputMapper.removeInputOfFactory(factoryId);

        Map<String,Object> map = new HashMap<>(1,1);
        map.put("processing_factory_id",factoryId);
        return inputMapper.deleteByMap(map);
    }

    //删除工艺与所有投入品的连接信息
    public int removeInputOfCraft(String craftId,String inputId){
        return inputMapper.removeInputOfCraft(craftId,inputId);
    }

    //删除指定工艺与其投入品的连接信息
    public int removeAllInputOfCraft(String craftId){
        return inputMapper.removeAllInputOfCraft(craftId);
    }

    //添加投入品到工艺中
    public int addInputOfCraft(String craftId,String inputId){
        return inputMapper.addInputOfCraft(craftId,inputId);
    }
}
